"""
普通版
"""
# import requests
# from urllib.parse import urljoin
#
# BASE_URL = 'https://login2.scrape.center/'
# LOGIN_URL = urljoin(BASE_URL, '/login')
# INDEX_URL = urljoin(BASE_URL, '/page/1')
# USERNAME = 'admin'
# PASSWORD = 'admin'
#
# response_login = requests.post(LOGIN_URL, data={
#     'username': USERNAME,
#     'password': PASSWORD
# }, allow_redirects=False)
#
# cookies = response_login.cookies
# print('Cookies', cookies)
#
# response_index = requests.get(INDEX_URL, cookies=cookies)
# print('Response Status', response_index.status_code)
# print('Response URL', response_index.url)
"""
Session版
"""
# import requests
# from urllib.parse import urljoin
#
# BASE_URL = 'https://login2.scrape.center/'
# LOGIN_URL = urljoin(BASE_URL, '/login')
# INDEX_URL = urljoin(BASE_URL, '/page/1')
# USERNAME = 'admin'
# PASSWORD = 'admin'
#
# session = requests.Session()
#
# response_login = session.post(LOGIN_URL, data={
#     'username': USERNAME,
#     'password': PASSWORD
# })
#
# cookies = session.cookies
# print('Cookies', cookies)
#
# response_index = session.get(INDEX_URL)
# print('Response Status', response_index.status_code)
# print('Response URL', response_index.url)
"""
Selenium版
"""
from urllib.parse import urljoin
from selenium import webdriver
import requests
import time

BASE_URL = 'https://login2.scrape.center/'
LOGIN_URL = urljoin(BASE_URL, '/login')
INDEX_URL = urljoin(BASE_URL, '/page/1')
USERNAME = 'admin'
PASSWORD = 'admin'

browser = webdriver.Chrome(executable_path="D:\chromedriver.exe")
browser.get(BASE_URL)
browser.find_element_by_css_selector('input[name="username"]').send_keys(USERNAME)
browser.find_element_by_css_selector('input[name="password"]').send_keys(PASSWORD)
browser.find_element_by_css_selector('input[type="submit"]').click()
time.sleep(10)

# get cookies from selenium
cookies = browser.get_cookies()
print('Cookies', cookies)
browser.close()

# set cookies to requests
session = requests.Session()
for cookie in cookies:
    session.cookies.set(cookie['name'], cookie['value'])

response_index = session.get(INDEX_URL)
print('Response Status', response_index.status_code)
print('Response URL', response_index.url)
